/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.om.entity.OmMaintenancePlan;
import com.excesys.exsecs.om.entity.OmMaintenancePlanTimes;
import com.excesys.exsecs.om.mapper.OmMaintenanceItemsMapper;
import com.excesys.exsecs.om.mapper.OmMaintenancePlanMapper;
import com.excesys.exsecs.om.mapper.OmMaintenancePlanTimesMapper;
import com.excesys.exsecs.om.service.OmMintenancePlanService;
import com.excesys.exsecs.om.utils.PlanToSheetUtils;
import com.excesys.exsecs.om.vo.MaintenancePlanExcelVo;
import com.excesys.exsecs.om.vo.MaintenancePlanVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class OmMintenancePlanServiceImpl extends ServiceImpl<OmMaintenancePlanMapper, OmMaintenancePlan>
		implements OmMintenancePlanService {

	private final OmMaintenancePlanTimesMapper omMaintenancePlanTimesMapper;

	private final OmMaintenancePlanMapper omMaintenancePlanMapper;

	private final PlanToSheetUtils planToSheetUtils;

	private final OmMaintenanceItemsMapper omMaintenanceItemsMapper;

	@Override
	public IPage<MaintenancePlanVo> getMintenancePlanPage(Page page, MaintenancePlanVo maintenancePlanVo) {
		return omMaintenancePlanMapper.getMintenancePlanWithRelPage(page, maintenancePlanVo);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveMintenancePlan(MaintenancePlanVo maintenancePlanVo) {
		OmMaintenancePlan omMintenancePlan = new OmMaintenancePlan();
		BeanUtil.copyProperties(maintenancePlanVo, omMintenancePlan);
		baseMapper.insert(omMintenancePlan);
		Optional.ofNullable(maintenancePlanVo.getTimesList()).ifPresent(timesList -> {
			timesList.stream().map(omMaintenancePlanTimes -> {
				omMaintenancePlanTimes.setParentId(omMintenancePlan.getId().toString());
				return omMaintenancePlanTimes;
			}).forEach(omMaintenancePlanTimesMapper::insert);
		});
		planToSheetUtils.generatorMintenanceTaskToday(omMintenancePlan);
		return Boolean.TRUE;
	}

	@Override
	public List<OmMaintenancePlan> getList() {
		return omMaintenancePlanMapper.getList();
	}

	@Override
	public List<MaintenancePlanExcelVo> listBamSBuildings() {
		List<OmMaintenancePlan> bamSBuildings = this.list(Wrappers.emptyWrapper());
		// 转换成execl 对象输出
		return bamSBuildings.stream().map(buildings -> {
			MaintenancePlanExcelVo maintenancePlanExcelVo = new MaintenancePlanExcelVo();
			BeanUtil.copyProperties(buildings, maintenancePlanExcelVo);
			return maintenancePlanExcelVo;
		}).collect(Collectors.toList());
	}

	@Override
	public OmMaintenancePlan execute(MaintenancePlanVo maintenancePlanVo) {
		Boolean flag = planToSheetUtils.executeMintenanceTaskToday(maintenancePlanVo);
		if (flag)
			maintenancePlanVo.setIsExecute("1");
		else
			maintenancePlanVo.setIsExecute("0");
		OmMaintenancePlan omMaintenancePlan = new OmMaintenancePlan();
		BeanUtil.copyProperties(maintenancePlanVo, omMaintenancePlan);
		baseMapper.updateById(omMaintenancePlan);
		return omMaintenancePlan;
	}

	@Override
	public R<Boolean> updatePlan(MaintenancePlanVo maintenancePlanVo) {
		OmMaintenancePlan omMaintenancePlan = new OmMaintenancePlan();
		BeanUtils.copyProperties(maintenancePlanVo, omMaintenancePlan);
		omMaintenancePlan.setUpdateTime(LocalDateTime.now());
		this.updateById(omMaintenancePlan);
		omMaintenancePlanTimesMapper.delete(Wrappers.<OmMaintenancePlanTimes>lambdaQuery()
				.eq(OmMaintenancePlanTimes::getId, maintenancePlanVo.getId()));

		// 保存用户岗位信息
		Optional.ofNullable(maintenancePlanVo.getTimesList()).ifPresent(timesList -> {
			timesList.stream().map(omMaintenancePlanTimes -> {
				omMaintenancePlanTimes.setParentId(maintenancePlanVo.getId().toString());
				return omMaintenancePlanTimes;
			}).forEach(omMaintenancePlanTimesMapper::insert);
		});
		return R.ok();
	}

}
